<template>
  <Container class="storesign-list">
    <HeadFormItem
      slot="header"
      :list="list"
      @onSearchBtnClick="onSearchBtnClick"
    />
    <Tabs v-model="tab">
      <div slot="extra">
        <Button size="large" class="item" type="primary" @click="onChangeOwner">维护人变更</Button>
        <Button size="large" class="item" type="primary" :disabled="selectedStoreIds.length<=0" @click="resignStore">批量转签</Button>
        <export-btn 
          :param="exportParam" 
          url='/contract_store/export'
          class="item" 
          slot="extra"></export-btn>
      </div>
      <TabPane label="门店签约列表" name="tab1">
        <homeTable :list="list" @getData="getData" @onPageSizeChange="onPageSizeChange" @onPageChange="onPageChange" @onSelectionChange="onSelectionChange" />
      </TabPane>
    </Tabs>
    <StoreResignModelItem ref="storeResign" />
    <OwerChangeModelItem ref="owerChange" />
    <PointModel ref="point-model" />
    <RebateModel ref="rebate-model" />
  </Container>
</template>
<script>
import SupplerSignListData from './list/StoreSignListData'
import HeadFormItem from './list/HeadFormItem'
import StoreResignModelItem from './list/StoreResignModelItem'
import OwerChangeModelItem from './list/OwerChangeModelItem'
// import SelectSearch from '@/components/SelectSearch'
import homeTable from '@/components/homeTable'
import PointModel from './list/PointModelItem'
import RebateModel from './list/RebateModelItem'

import { clearEmpty } from '@/util'
export default {
  name: 'manage/storesign/list',
  components: {
    HeadFormItem,
    // SelectSearch,
    homeTable,
    StoreResignModelItem,
    OwerChangeModelItem,
    PointModel,
    RebateModel
  },
  data () {
    return {
      tab: 'tab1',
      list: SupplerSignListData,
      selectedStoreIds: []
    }
  },
  watch: {
  },
  mounted () {
    const menu = JSON.parse(localStorage.getItem('menu') || '[]')
    this.list.isIntegralRight = !!menu.find(item => item.viewUrl === '/assets/store_reward/integral/reward' && item.hasRight)
    this.list.isRebateRight = !!menu.find(item => item.viewUrl === '/assets/store_reward/rebate/reward' && item.hasRight)
    this.getData()
    this.$bus.$on('detailRow', this.signInfo)
    this.$bus.$on('editRow', this.signEdit)
    this.$bus.$on('changeStar', this.changeStar)
    this.$bus.$on('pointEdit', ({ row }) => { this.pointEdit(row) }) // 积分奖惩
    this.$bus.$on('rebateEdit', ({ row }) => { this.rebateEdit(row) }) // 返利奖惩
  },
  beforeDestroy () {
    this.$bus.$off()
  },
  methods: {
    onSearchBtnClick () {
      this.list.currentPage = 1
      this.getData()
    },
    changeStar (row) {
      this.$ajaxPost(`/contract_store/setStar/${row.id}`, { contractId: row.id, isStar: !row.isStar }).then(res => {
        row.isStar = !row.isStar
      })
    },
    signEdit (row) {
      this.$router.push({
        name: 'manage/storesign/edit',
        query: {
          id: row.id
        }
      })
    },
    pointEdit (row) {
      this.$refs['point-model'].updataForm(row.id)
    },
    rebateEdit (row) {
      this.$refs['rebate-model'].updataForm(row.id)
    },
    signInfo (row) {
      this.$router.push({
        name: 'manage/storesign/info',
        query: {
          id: row.id
        }
      })
    },
    resignStore () {
      this.$refs.storeResign.openModal(this.selectedStoreIds)
    },
    onChangeOwner () {
      this.$refs.owerChange.openModal()
    },
    getData () {
      this.selectedStoreIds = []
      this.list.status = 'loading'
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        ...this.list.form
      }
      param = clearEmpty(param)
      this.$jsonPost('/contract_store/page', param).then(data => {
        this.list.data = []
        this.list.totalItem = Number(data.total)
        this.list.data = (data.records || []).map(item => {
          item.isIntegralRight = this.list.isIntegralRight
          item.isRebateRight = this.list.isRebateRight
          return item
        })
        this.list.status = 'pass'
      })
        .catch(() => {
          this.list.status = 'error'
        })
    },
    onSelectionChange (selection) {
      this.selectedStoreIds = (selection || []).map(item => item.id)
    },
    onPageChange (number) {
      this.list.currentPage = number
      this.getData()
    },
    onPageSizeChange (pageSize) {
      this.list.currentPage = 1
      this.list.pageSize = pageSize
      this.getData()
    },
    onRefresh () {
      this.getData()
    }
  },
  computed: {
    exportParam() {
      return {...this.list.form}
    }
  }
}
</script>
